package cn.gs.controller.lambdaTest;

import cn.gs.entity.User;
import cn.gs.service.Factory;

public class LambdaTest {

    public static void main(String[] args) {
        /**
         * 传统多态方法的实现
         */
        Factory fac01 =new ObjectClass();
        User user01 = (User) fac01.getObject();
        user01.setName("张三");
        user01.setAge(27);
        System.out.println(user01.toString());
        /**
         * 接口匿名内部类的实现
         */
        Factory fac02 = new Factory(){
            @Override
            public Object getObject() {
                return new User("李四",28);
            }
        };
        User user02 = (User) fac02.getObject();
        System.out.println(user02.toString());
        /**
         * lambda的实现
         */
        Factory fac03 = () -> {
            return new User("王五",29);
        };
        User user03 = (User) fac03.getObject();
        System.out.println(user03.toString());
        // lambda简化写法
        Factory fac04 = () ->  new User("马六",30);
        User user04 = (User) fac04.getObject();
        System.out.println(user04.toString());
        /**
         * Runnable接口的匿名内部类实现
         */
        new Thread (new Runnable(){
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName();
                System.out.println(threadName + " 开始运行！");
            }
        }).start();
        /**
         * Runnable接口的lambda实现
         */
        new Thread( ()->{
            String threadName02 = Thread.currentThread().getName();
            System.out.println(threadName02 + " 开始运行！");
        } ).start();
    }

    /**   cfr-0.145.jar  查看lambda编译过程，  java -jar cfr-0.145.jar LambdaTest.class --decodelambdas false
     *  java -p -v LambdaTest.class 反编译
     *
     public static void main(String[] args) {
     ObjectClass fac01 = new ObjectClass();
     User user01 = (User)fac01.getObject();
     user01.setName("\u5f20\u4e09");
     user01.setAge(27);
     System.out.println(user01.toString());
     Factory fac02 = new Factory(){

     public Object getObject() {
     return new User("\u674e\u56db", 28);
     }
     };
     User user02 = (User)fac02.getObject();
     System.out.println(user02.toString());
     Factory fac03 = (Factory)LambdaMetafactory.metafactory(null, null, null, ()Ljava/lang/Object;, lambda$main$0(), ()Ljava/lang/Object;)();
     User user03 = (User)fac03.getObject();
     System.out.println(user03.toString());
     Factory fac04 = (Factory)LambdaMetafactory.metafactory(null, null, null, ()Ljava/lang/Object;, lambda$main$1(), ()Ljava/lang/Object;)();
     User user04 = (User)fac04.getObject();
     System.out.println(user04.toString());
     new Thread(new Runnable(){

    @Override
    public void run() {
    String threadName = Thread.currentThread().getName();
    System.out.println(threadName + " \u5f00\u59cb\u8fd0\u884c\uff01");
    }
    }).start();
     new Thread((Runnable)LambdaMetafactory.metafactory(null, null, null, ()V, lambda$main$2(), ()V)()).start();
     }

     private static  void lambda$main$2() {
        String threadName02 = Thread.currentThread().getName();
        System.out.println(threadName02 + " \u5f00\u59cb\u8fd0\u884c\uff01");
    }

    private static  Object lambda$main$1() {
        return new User("\u9a6c\u516d", 30);
    }

    private static  Object lambda$main$0() {
        return new User("\u738b\u4e94", 29);
    }
     */

}
